# Adogs
# 时间: 2021/8/1 14:46
import pygame
from game import SCREEN_WIDTH
from utils.text_render import blit_text


class SysBaseDialog:
    """
    系统基本对话
    方法：
    1.__init__(text_list, font)
    2.draw(surface)
    3.is_finish()
    4.next_text()
    """
    def __init__(self, text_list: list, font):
        """
        初始化函数
        :param text_list: 当前阶段的对话文本
        :param font: 字体
        """
        self.text_list = text_list
        self.current_id = 0     # 第几段文本
        self.dialog_image = pygame.image.load("resources/picture/text_box/sys_dialog.png")  # 对话框图片
        x, y, w, h = self.dialog_image.get_rect()

        # 对话框位置
        self.pos_x = int((SCREEN_WIDTH - w) / 2) + 35
        self.pos_y = 300

        # 对话是否结束
        self.finish = False

        self.font = font

    def draw(self, surface):
        """
        绘制函数
        :param surface: 绘制屏幕
        :return: 无返回值
        """
        if self.current_id >= len(self.text_list):  # 文本播放结束就退出
            return
        else:
            surface.blit(self.dialog_image, (self.pos_x, self.pos_y))
            blit_text(surface, self.text_list[self.current_id], (self.pos_x + 30, self.pos_y + 35), self.font)

    def is_finish(self):
        """
        判断对话是否结束
        :return: True or Fasle
        """
        return self.finish

    def next_text(self):
        """
        播放下一段对话
        :return: 无返回值
        """
        self.current_id += 1
        if self.current_id >= len(self.text_list):  # 对话播放完毕
            self.finish = True
            self.current_id = len(self.text_list)


class ActorBaseDialog(SysBaseDialog):
    """
    人物基本对话
    方法：
    1.__init__(actor, text_list, font)
    2.draw(surface, view_x, view_y)
    """
    def __init__(self, actor, text_list: list, font):
        """
        初始化函数
        :param actor: 显示对话的人物
        :param text_list: 对话文本
        :param font: 字体
        """
        self.text_list = text_list
        self.current_id = 0
        self.dialog_image = pygame.image.load("resources/picture/text_box/actor_dialog.png")    # 对话框图片
        self.actor = actor
        self.finish = False
        self.font = font

    def draw(self, surface, view_x, view_y):
        """
        绘制函数
        :param surface: 绘制屏幕
        :param view_x: 屏幕相对x坐标
        :param view_y: 屏幕相对y坐标
        :return: 无返回值
        """
        if self.current_id >= len(self.text_list):  # 对话播放完毕
            return

        # 对话框显示位置
        temp_x = self.actor.pos_x - view_x + 5
        temp_y = self.actor.pos_y - view_y - 55

        surface.blit(self.dialog_image, (temp_x, temp_y))
        blit_text(surface, self.text_list[self.current_id], (temp_x + 10, temp_y + 10), self.font)
